package light.mvc.controller.hrs;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import light.mvc.controller.base.BaseController;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.hrs.HRSMember;
import light.mvc.pageModel.sys.Role;
import light.mvc.service.hrs.HRSMemberServiceI;
import light.mvc.service.sys.RoleServiceI;


@Controller
@RequestMapping("/member")
public class HRSMemberController extends BaseController{

	@Autowired 
	private HRSMemberServiceI memberService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/habitus_research_sys/member";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(HRSMember member, PageFilter ph) {
//		spring自动将参数注入到ph对象中
		Grid grid = new Grid();
		grid.setRows(memberService.dataGrid(member, ph));
		grid.setTotal(memberService.count(member, ph));
		return grid;
	}
	
	@RequestMapping("/addPage")
	public String addPage() {
		return "/admin/habitus_research_sys/memberAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(HRSMember member) {
		Json j = new Json();
		HRSMember m = memberService.getByLoginName(member);
		if(m!=null){
			j.setMsg("用户名已存在!");
		}else{
			try {
				memberService.add(member);
				j.setSuccess(true);
				j.setMsg("添加成功！");
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
//	ids为jsonArray
	public Json delete(String ids) {
		JSONArray jsonIds;
		Json j = new Json();
		try {
			jsonIds = new JSONArray(ids);
			for (int i = 0; i < jsonIds.length(); i++) {
				memberService.delete(jsonIds.getLong(i));
			}
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		HRSMember m = memberService.get(id);
		request.setAttribute("member", m); 
		request.setAttribute("statusList",GlobalConstant.statuslist);
		return "/admin/habitus_research_sys/memberEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(HRSMember member) {
		Json j = new Json();
		HRSMember m = memberService.getByLoginName(member);
		if(m!=null&&m.getId()!=member.getId()){
			j.setMsg("用户名已存在!");
		}else{
			try {
				memberService.edit(member);
				j.setSuccess(true);
				j.setMsg("编辑成功！");
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
		}
		return j;
	}
	
	@RequestMapping("/verifyPage")
	public String verifyPage() {
		return "/admin/habitus_research_sys/memberVerify";
	}

	@RequestMapping("/pass")
	@ResponseBody
	public Json pass(String ids) {
		JSONArray jsonIds;
		Json j = new Json();
		try {
			jsonIds = new JSONArray(ids);
			for (int i = 0; i < jsonIds.length(); i++) {
				memberService.pass(jsonIds.getLong(i));
			}
			j.setMsg("通过审核成功！");
			j.setSuccess(true);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

}
